package com.cqsw.service.impl;

import com.cqsw.service.MailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * @Classname MailServiceImpl
 * @Description TODO 邮件发送实现类
 * @Date 2022/6/14 9:12
 * @Created by Smith.Liu
 */
@Service
@Slf4j
public class MailServiceImpl implements MailService {

    @Autowired
    private MailProperties mailProperties;
    @Autowired
    private JavaMailSender javaMailSender;


    /***
     * 发送普通邮件
     * @param to
     * @param subject
     * @param content
     * @return
     */
    @Override
    public boolean send(String to, String subject, String content) {
        log.info("=======================开始发送短信========================");
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        //邮件发送来源
        simpleMailMessage.setFrom(mailProperties.getUsername());
        //设置标题
        simpleMailMessage.setSubject(subject);
        //设置内容
        simpleMailMessage.setText(content);
        try {
            javaMailSender.send(simpleMailMessage);
            log.info("============================Send the mail success=============================");
        } catch (Exception e) {
            log.info("============================Send the mail failed=============================");
            return false;
        }
        return true;
    }

    /**
     * 发送html格式文件
     *
     * @param to
     * @param subject
     * @param html
     * @return
     */
    @Override
    public boolean sendWithHtml(String to, String subject, String html) {
        log.info("##  Ready to send mail . . . ");
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = null;
        try {
            mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setFrom(mailProperties.getUsername());
            mimeMessageHelper.setTo(to);
            mimeMessageHelper.setSubject(subject);
            mimeMessageHelper.setText(html, true);
            javaMailSender.send(mimeMessage);
            log.info("## send the mail with image success");
        } catch (MessagingException e) {
            log.error("Send html mail error: ", e);
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean sendWithImageHtml(String to, String subject, String html, String[] cids, String[] filePaths) {
        return false;
    }

    @Override
    public boolean sendWithWithEnclosure(String to, String subject, String content, String[] filePaths) {
        return false;
    }
}
